home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia 1995 April / Informatica Multimedia CD - Epimundo.iso / DOS / FILE_CHG / AWK.ZIP / AWK.DOC < prev    next >
Encoding:
Text File  |  1988-03-05  |  28.6 KB  |  828 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.               AWK - A pattern Scanning and Processing Language
  7.                               (Second Edition)
  8.  
  9.                                Alfred V. Aho
  10.                              Brian W. Kernighan
  11.                             Peter J. Weinberger
  12.  
  13.                              Bell Laboratories
  14.                        Murray Hill, New Jersey 07974
  15.  
  16.  
  17.  
  18.                                   ABSTRACT
  19.  
  20.      Awk is a programming language whose basic operation is to search a set 
  21. of files for patterns, and to perform specified actions upon lines or fields 
  22. of lines which contain instances of those patterns.  Awk makes certain data 
  23. selection and transformation operations easy to express; for example, the 
  24. awk program
  25.  
  26.                                 length > 72
  27.  
  28. prints all input lines whose length exceeds 72 characters; the program
  29.  
  30.                                 NF % 2 == 0
  31.  
  32. prints all lines with an even number of fields; and the program
  33.  
  34.                           { $1 = log($1); print }
  35.  
  36. replaces the first field of each line by its logarithm.
  37.  
  38.      Awk patterns may include arbitrary boolean combinations of regular 
  39. expresssions and of relational operators on strings, numbers, fields, 
  40. variables and array elements.  Actions may include the same pattern-
  41. matching constructions as in patterns, as well as arithmetic and string 
  42. expressions and assignments, if-else, while, for statements, and multiple 
  43. output streams.
  44.  
  45.      This report contains a user's guide, a discussion of the design and 
  46. implementation of awk, and some timing statistics. 
  47.  
  48. September 1, 1978 
  49.  
  50.  
  51.  
  52.  
  53.  
  54.               AWK - A Pattern Scanning and Processing Language
  55.                               (Second Edition)
  56.  
  57.                                Alfred V. Aho
  58.                              Brian W. Kernighan
  59.                             Peter J. Weinberger
  60.  
  61.                              Bell Laboratories
  62.                        Murray Hill, New Jersey 07974
  63.  
  64.  
  65.  
  66.  
  67.  
  68. 1. Introduction
  69.  
  70.      Awk is a programming language designed to make many common information 
  71. retrieval and text manipulation tasks easy to state and to perform. 
  72.  
  73.      The basic operation of awk is to scan a set of input lines in order, 
  74. searching for lines which match any of a set of pastterns which the user 
  75. has specified.  For each pattern, an action can be specified; this action 
  76. will be performed on each line that matches the pattern. 
  77.  
  78.      Readers familiar with the UNIX[*] program grep[1] will recognize the 
  79. approach, although in awk the patterns may be more general than in grep, 
  80. and the actions allowed are more involved than merely printing the matching 
  81. line.  For example the awk program 
  82.  
  83.         { print $3, $2 }
  84.  
  85. prints the third and second columns of a table in that order.  The program 
  86.  
  87.         $2 ~ /A|B|C/
  88.  
  89. prints all input lines with an A, B, or C in the second field.  The program 
  90.  
  91.         $1 != prev { print; prev = $1 }
  92.  
  93. prints all lines in which the first field is different from the previous 
  94. first field. 
  95.  
  96. 1.1.  Usage
  97.  
  98. The command
  99.  
  100.         awk  program [files]
  101.  
  102. executes the awk commands in the string program on the set of named files, 
  103. or on the standard input if there are no files.  The statements can also be 
  104. placed in a file pfile, and executed by the command 
  105.  
  106.         awk  -f pfile [files]
  107.  
  108. 1.2.  Program Structure
  109.  
  110.      An ask program is a sequence of statements of the form: 
  111.  
  112.         pattern      { action }
  113.         pattern      { action }
  114.         ...
  115.  
  116. Each line of input is matched against each of the patterns in turn.  For 
  117. each pattern that matches the associated action is executed.  When all the 
  118. patterns have been tested, the next line is fetched and the matching starts 
  119. over. 
  120.  
  121.      Either the pattern or the action may be left out, but not both.  If 
  122. there is no action for the pattern, the matching line is simply copied to 
  123. the output.  (Thus a line which matches several patterns can be printed 
  124. several times.)  If there is no pattern for an action, then the action is 
  125. performed for every input line.  A line which matches no pattern is 
  126. ignored. 
  127.  
  128.      Since patterns and actions are both optional, actions must be enclosed 
  129. in braces to distinguish them from patterns. 
  130.  
  131. 1.3.  Records and Fields
  132.  
  133.      Awk input is divided in to "records" terminated by a record separator.  
  134. The default record separator is a newline, so by default awk processes its 
  135. input a line at a time.  The number of the current record is available in a 
  136. variable named NR. 
  137.  
  138.      Each input record is concidered to be divided into "fields."  Fields 
  139. are normally separated by white space - blanks or tabs - but the input 
  140. field separator may be changed, as described below.  Fields are referred to 
  141. as $1, $2, and so forth, where $1 is the first field, and $0 is the whole 
  142. input record itself.  Fields may be assigned to.  The number of fields in 
  143. the current record is available in a variable named NF. 
  144.  
  145.      The variables FS and RS refer to the input field and record 
  146. separators; they may be changed at any time to a single character.  The 
  147. optional command line argument -Fc may also be used to set the FS to the 
  148. character c. 
  149.  
  150.      If the record separator is empty, an empty input line is taken as the 
  151. record separator, and blanks, tabs and newlines are treated as field 
  152. separators. 
  153.  
  154.      The variable FILENAME contains the name of the current input file. 
  155.  
  156. 1.4.  Printing
  157.  
  158.      An action may have no pattern, in which case the action is executed 
  159. for all lines.  The simplest action is to print some or all of a record; 
  160. this is accomplished by the awk command print.  The awk program 
  161.  
  162.         { print }
  163.  
  164. prints each record, thus copying the input to the output intact.  More 
  165. useful is to print a field or fields from each record.  For instance, 
  166.  
  167.         print $2, $1
  168.  
  169. prints the first two fields in reverse order.  Items separated by a comma 
  170. in the print statement will be separated by the current output field 
  171. separator when output.  Items not separated by commas will be concatenated, 
  172. so 
  173.  
  174.         print $1 $2
  175.  
  176. runs the first and second fields together.
  177.  
  178.      The predefined variables NF and NR can be used; for example 
  179.  
  180.         { print NR, NF, $0 }
  181.  
  182. prints each record preceded by the record number and the number of fields. 
  183.  
  184.      Output may be diverted to multiple files; the program 
  185.  
  186.         { print $1 >"foo1"; print $2 >"foo2" }
  187.  
  188. writes the first field, $1 on the file foo1 and the second field on the 
  189. file foo2.  The >> notation can also be used: 
  190.  
  191.         print $1 >>"foo"
  192.  
  193. appends the output to the file foo.  (In each case, the output files are 
  194. created if necessary.)  The file name can be a variable or a field as well 
  195. as a constant; for example, 
  196.  
  197.         print $1 >$2
  198.  
  199. uses the contents of field 2 as a file name.
  200.  
  201.      Naturally there is a limit on the number of output files; currently it 
  202. is 10. 
  203.  
  204.      Similarly, output can be piped into another process (on UNIX only); 
  205. for instance, 
  206.  
  207.         print | "mail bwk"
  208.  
  209. mails the output to bwk.
  210.  
  211.      The variables OFS and ORS may be used to change the current output 
  212. field separator and output record separator.  The output record separator 
  213. is appended to the output of the print statement. 
  214.  
  215.      Awk also provides the printf statement for ouput formatting: 
  216.  
  217.         printf "%8.2f  %10ld\n", $1, $2
  218.  
  219. prints $1 as a floating point number 8 digits wide, with two after the 
  220. decimal point, and $2 as a 10-digit long decimal number, followed by a 
  221. newline.  No output separators are produced automatically; you must add 
  222. them yourself, as in this example.  The version of printf is identical to 
  223. that used with C[2]. 
  224.  
  225. 2.  Patterns
  226.  
  227.      A pattern in front of an action acts as a selector that determines 
  228. whether the action is to be executed.  A variety of expressions may be used 
  229. as patterns; regular expressions, arithmetic relational expressions, 
  230. string-valued expressions, and arbitrary boolean combinations of these. 
  231.  
  232. 2.1.  BEGIN and END
  233.  
  234.      The special pattern BEGIN matches the beginning of the input, before 
  235. the first record is read.  The pattern END matches the end of the input, 
  236. after the last record has been processed.  BEGIN and END thus provide a way 
  237. to gain control before and after processing, for initialization and wrapup. 
  238.  
  239.      As an example, the field separator can be set to a colon by
  240.  
  241.         BEGIN { FS = ":" }
  242.         ... rest of program ...
  243.  
  244. Or the input lines may be counted by
  245.  
  246.         END { print NR }
  247.  
  248. If BEGIN is present, it must be the first pattern; END must be the last if 
  249. used.
  250.  
  251. 2.2.  Regular Expressions
  252.  
  253.      The simplest regular expression is a literal string of characters 
  254. enclosed in slashes, like 
  255.  
  256.         /smith/
  257.  
  258. This is actually a complete awk program which will print any occurances of 
  259. the name "smith".  If a line contains "smith" as part of a larger word, it 
  260. will also be printed, as in 
  261.  
  262.         blacksmithing
  263.  
  264.      Awk regular expressions include the regular expression forms found in 
  265. the UNIX text editor ed[1] and grep (without back referencing).  In 
  266. addition, awk allows parentheses for grouping, | for alternatives, + for 
  267. "one or more", and ? for "zero or one", all as in lex[3].  Character 
  268. classes may be abbreviated: [a-zA-Z0-9] is the set of all letters and 
  269. digits.  As an example, the awk program 
  270.  
  271.         /[Aa]ho|[Ww]einberger|[Kk]ernighan/
  272.  
  273. will print all lines which contain any of the names "Aho," "Weinberger" or 
  274. "Kernighan," whether capitalized or not. 
  275.  
  276.      Regular expressions (with the extensions listed above) must be 
  277. enclosed in slashes, just as in ed and sed[1].  Within  a regular 
  278. expression, blanks and the regular expression metacharacters are 
  279. signifigant.  To turn off the magic meaning of one of the regular 
  280. expression characters, precede it with a backslash.  An example is the 
  281. pattern 
  282.  
  283.         /\/.*\//
  284.  
  285. which matches any string of characters enclosed in slashes. 
  286.  
  287.      One can also specify that any field or variable matches a regular 
  288. expression (or does not match it) with the operators ~ and !~.  The program 
  289.  
  290.         $1 ~ /[jJ]ohn/
  291.  
  292. prints all lines where the first field matches "john" or "John."  Notice 
  293. that this will also match "Johnson", "St. Johnsbury", and so on.  To 
  294. restrict it to exactly "John" use 
  295.  
  296.         $1 ~ /^[jJ]ohn$/
  297.  
  298. The caret ^ refers to the beginning of a line or field;  the dollar sign $ 
  299. refers to the end. 
  300.  
  301. 2.3.  Relational Expressions
  302.  
  303.      An awk pattern can be a relational expression involving the usual 
  304. relational operators <, <=, ==, !=, >=, and >.  An example is 
  305.  
  306.         $2 > $1 + 100
  307.  
  308. which selects lines where the second field is at least greater than the 
  309. first field.  Similarly 
  310.  
  311.         NF % 2 == 0
  312.  
  313. prints all lines with an even number of fields.
  314.  
  315.      In relational tests, if neither operand is numeric, a string 
  316. comparison is made; otherwise it is numeric.  Thus 
  317.  
  318.         $1 >= "s"
  319.  
  320. selects lines that begin with an s, t, u, etc.  In the absence of any other 
  321. information, fields are treated as strings, so the program 
  322.  
  323.         $1 > $2
  324.  
  325. will perform a string comparison.
  326.  
  327. 2.4.  Combinations of Patterns
  328.  
  329.      A pattern can be any boolean combination of patterns, using the 
  330. operators || (or), && (and), and ! (not).  For example, 
  331.  
  332.         $1 >= "s" && $1 < "t" && $1 != "smith"
  333.  
  334. selects lines where the first field begins with "s" but is not "smith".  && 
  335. and || guarantee that their operands will be evaluated from left to right;  
  336. evaluation stops as soon as the truth or falsehood is determined. 
  337.  
  338. 2.5.  Pattern Ranges
  339.  
  340.      The "pattern" that selects an action may also consist of two patterns 
  341. separated by commas, as in 
  342.  
  343.         pat1, pat2      { ... }
  344.  
  345. In this case, the action is performed for each line between an occurance of 
  346. pat1 and the next occurance of pat2 (inclusive).  For example, 
  347.  
  348.         /start/,/stop/
  349.  
  350. prints all lines between start and stop, while
  351.  
  352.         NR == 100, NR == 200 { ... }
  353.  
  354. does the action for lines 100 through 200 of the input.
  355.  
  356. 3.  Actions
  357.  
  358.      An awk action is a sequence of action statements terminated by 
  359. newlines or semicolons.  These action statements can be used to do a 
  360. variety of bookkeeping and string manipulating tasks. 
  361.  
  362. 3.1.  Built-in Functions
  363.  
  364.      Awk provides a "length" function to compute the length of a string of 
  365. characters.  This program prints each record, preceded by its length: 
  366.  
  367.         { print length, $0 }
  368.  
  369. length by itself is a "pseudo-variable" which yeilds the length of the 
  370. current record; length(argument) is a function which yeilds the length of 
  371. its argument, as in the equivalent 
  372.  
  373.         { print length($0), $0 }
  374.  
  375. The argument may be any expression.
  376.  
  377.      Awk also provides the arithmetic functions sqrt, log, exp, and int, 
  378. for square root, base e logarithm, exponential, and integer parts of their 
  379. respective arguments. 
  380.  
  381.      The name of one of these built-in functions without arguments or 
  382. parentheses, stands for the value of the function on the whole record.  The 
  383. program 
  384.  
  385.         length < 10 || length > 20
  386.  
  387. prints lines whose length is less than 10 or more than 20.
  388.  
  389.      The function substr(s, m, n) produces the substring of s that begins 
  390. at position m (origin 1) and is at most n characters long.  If n is 
  391. omitted, the substring goes to the end of s.  The function index(s1, s2) 
  392. returns the position where the string s2 occurs in s1, or zero if it does 
  393. not. 
  394.  
  395.      The function sprintf(f, e1, e2, ...) produces the value of the 
  396. expressions e1, e2, etc, in the printf format specified by f.  Thus for 
  397. example, 
  398.  
  399.         x = sprintf("%8.2f %10ld", $1, $2)
  400.  
  401. sets x to the string produced by formatting the values of $1 and $2.
  402.  
  403. 3.2.  Variables, Expressions, and Assignments
  404.  
  405.      Awk variables take on numeric (floating point) or string values 
  406. according to context.  For example, in 
  407.  
  408.         x = 1
  409.  
  410. x is clearly a number, while in
  411.  
  412.         x = "smith"
  413.  
  414. it is clearly a string.  Strings are converted to numbers and vice versa 
  415. whenever context demands it.  For instance, 
  416.  
  417.         x = "3" + "4"
  418.  
  419. assigns 7 to x.  Strings which cannot be interpreted as numbers in a 
  420. numerical context will generally have numeric value zero, but it is unwise 
  421. to count on this behavior. 
  422.  
  423.       By default, variables (other than built-ins) are initialized to the 
  424. null string, which has numerical value zero;  this eliminates the need for 
  425. most BEGIN sections.  For example, the sums of the first two fields can be 
  426. computed by 
  427.  
  428.             { s1 += $1; s2 += $2 }
  429.         END { print s1, s2 }
  430.  
  431.      Arithmetic is done internally in floating point.  The arithmetic 
  432. operators are +, -, *, /, and % (mod).  The C increment ++ and decrement -- 
  433. operators are also available, and so are the assignment operators +=, -=, 
  434. *=, /=, and %=.  These operators may all be used in expressions. 
  435.  
  436. 3.3.  Field Variables
  437.  
  438.      Fields in awk share essentially all of the properties of variables - 
  439. they may be used in arithmetic or string operations, and may be assigned 
  440. to.  Thus one can replace the first field with a sequence number like this: 
  441.  
  442.         { $1 = NR; print }
  443.  
  444. or accumulate two fields into a third, like this:
  445.  
  446.         { $1 = $2 + $3; print $0 }
  447.  
  448. or assign a string to a field:
  449.  
  450.         {   if ($3 > 1000)
  451.                 $3 = "too bit"
  452.             print
  453.         }
  454.  
  455. which replaces the third field by "too big" when it is, and in any case 
  456. prints the record. 
  457.  
  458.      Field references may be numerical expressions, as in
  459.  
  460.         { print $i, $(i+1), $(i+n) }
  461.  
  462. Whether a field is deemed numeric or string depends on context; in 
  463. ambiguous cases like 
  464.  
  465.         if ($1 == $2) ...
  466.  
  467. fields are treated like strings.
  468.  
  469.      Each input line is split into fields automatically as necessary.  It 
  470. is also possible to split an variable or string into fields: 
  471.  
  472.         n = split(s, array, sep)
  473.  
  474. splits the string s into array[1], ..., array[n].  The number of elements 
  475. found is returned.  If the sep argument is provided, it is used as the 
  476. field separator; otherwise FS is used as the separator. 
  477.  
  478. 3.4.  String Concatenation
  479.  
  480.      Strings may be concatenated.  For example
  481.  
  482.         length($1 $2 $3)
  483.  
  484. returns the length of the first three fields.  Or in a print statement,
  485.  
  486.         print $1 " is " $2
  487.  
  488. prints the two fields separated by " is ".  Variables and numeric 
  489. expressions may also appear in concatenations. 
  490.  
  491. 3.5.  Arrays
  492.  
  493.      Array elements are not declared; they spring into existence by being 
  494. mentioned.  Subscripst may have any non-null value, including non-numeric 
  495. strings.  As an example of a conventional numeric subscript, the statement 
  496.  
  497.         x[NR] = $0
  498.  
  499. assigns the current input record to the NR-th element of the array x.  In 
  500. fact, it is possible in principle (although perhaps slow) to process the 
  501. entire input in a random order with the awk program 
  502.  
  503.                 { x[NR] = $0 }
  504.         END     { ... program ... }
  505.  
  506. The first actio merely records each input line in the array x.
  507.  
  508.      Array elements may be named by non-numeric values, which gives awk the 
  509. capability rather like the associative memory of Snobol tables.  Suppose 
  510. the input contains fields with values like apple, orange, etc.  Then the 
  511. program 
  512.  
  513.         /apple/  { x["apple"]++ }
  514.         /orange/ { x["orange"]++ }
  515.         END      { print x["apple"], x["orange"] }
  516.  
  517. increments counts for the named array elements, and prints them at the end 
  518. of the input. 
  519.  
  520. 3.6.  Flow-of-Control Statements
  521.  
  522.     Awk provides the basic flow-of-control statements if-else, while, for, 
  523. and statement grouping within braces, as in C.  We showed the if statement 
  524. in section 3.3 without describing it.  The condition i parentheses is 
  525. evaluated; if it is true, the statement following the if is done.  The else 
  526. part is optional. 
  527.  
  528.     The while statement is exactly like that of C.  For example, to print 
  529. all input fields one per line, 
  530.  
  531.         i = 1
  532.         while (i <= NF) {
  533.                 print $i
  534.                 ++i;
  535.         }
  536.  
  537.      The for statement is exactly that of C;
  538.  
  539.         for (i = 1; i <= NF; i++ )
  540.                 print $i
  541.  
  542. does the same job as the while statement above.
  543.  
  544.      There is an alternate form of the for statement which is suited for 
  545. accessing the elements of an associative array: 
  546.  
  547.         for (i in array)
  548.                 statement
  549.  
  550. does statement with i set in turn to each element of array.  The elements 
  551. are accessed in an apparently random order.  Chaos will ensue if i is 
  552. altered, or if any new elements are accessed during the loop. 
  553.  
  554.      The expression in the condition part of an if, while or for can 
  555. include relational operators like <, <=, >, >=, == ("is equal to"), and != 
  556. ("not equal to"); regular expression matches with the match operators ~ and 
  557. !~; the logical operators ||, &&, and !; and of course parentheses for 
  558. grouping. 
  559.  
  560.      The break statement causes an immediate exit from an enclosing while 
  561. or for; the continue statement causes the next iteration to begin. 
  562.  
  563.      The statement next causes awk to skip immediately to the next record 
  564. and begin scanning from the top.  The statement exit causes the program to 
  565. behave as if the end of the input had occured. 
  566.  
  567.      Comments may be placed in awk programs; they begin with the character 
  568. # and end with the end of the line, as in
  569.  
  570.         print x, y # this is a comment
  571.  
  572. 4.  Design
  573.  
  574.      The UNIX system already provides several programs that operate by 
  575. passing input through a selection mechanism.  Grep, the first and simplest, 
  576. merely prints all lines which match a single specified pattern.  Egrep 
  577. provides more general patterns, i.e., regular expressions in full 
  578. generality; fgrep searches for a set of keywords with a particularly fast 
  579. algorithm.  Sed provides most of the editing facilities of the editor ed, 
  580. applied to a stream of input.  None of these programs provides numeric 
  581. capabilities, logical relations, or variables. 
  582.  
  583.      Lex provides general regular expression recognition capabilities, and, 
  584. by serving as a C program generator, is essentially open-ended in its 
  585. capabilities.  The use of lex, however, requires a knowledge of C 
  586. programming, and a lex program must be compiled and loaded before use, 
  587. which discourages its use for one-shot applications. 
  588.  
  589.      Awk is an attempt to fill in another part of the matrix of 
  590. possibilities.  It provides general regular expression capabilities and an 
  591. implicit input/output loop.  But it also provides convenient numeric 
  592. processing, variables, more general selection, and control flow in the 
  593. actions.  It does not rerquire compilation or a knowledge of C.  Finally 
  594. awk provides a convenient way to access fields within lines; it is unique 
  595. in this respect. 
  596.  
  597.      Awk also tries to integrate strings and numbers completely, by 
  598. treating all quantities as both string and numeric, deciding which 
  599. representation is appropriate as late as possible.  In most cases the user 
  600. can simply ignore the differences. 
  601.  
  602.      Most of the effort in developing awk went into deciding what awk 
  603. should or should not do (for instance, it doesn't do string substitution) 
  604. and what the syntax should be (no explicit operator for concatenation) 
  605. rather than on writing or debugging the code.  We have tried to make the 
  606. syntax powerful but easy to use and well adapted to scanning files.  For 
  607. example, the absence of declarations and implicit initializations, while 
  608. probably a bad idea for a general-purpose programming language, is 
  609. desirable in a language that is meant to be used for tiny progrmas that may 
  610. even be composed on the command line. 
  611.  
  612.      In practice, awk usage seems to fall into two broad catagories.  One 
  613. is what might be called "report generation" - processing an input to 
  614. extract counts, sums, sub-totals, etc.  This also includes the writing of 
  615. trivial data validation programs, such as verifying that a field contains 
  616. only numeric informataion or that certain delimiters are properly balanced.  
  617. The combination of textual and numeric processing is invaluable here. 
  618.  
  619.      A second area of use is as a data transformer, converting data from 
  620. the form produced by one program into that expected by another.  The 
  621. simplest examples merely select fields, perhaps with rearrangements. 
  622.  
  623. 5. Implementation
  624.  
  625.      The acutal implementation of awk uses the language development tools 
  626. available on the UNIX operating system.  The grammar is specified with 
  627. yacc[4]; the lexical analysis is done by lex; the regular expression 
  628. recognizers are deterministic finite automata constructed directly from the 
  629. expressions.  An awk program is translated into a parse tree which is then 
  630. executed by a simple interpreter. 
  631.  
  632.      Awk was designed for ease of use rather than processing speed; the 
  633. delayed evaluation of variable types and the necessity to break input into 
  634. fields makes high speed difficult to achieve in any case.  Nontheless, the 
  635. program has not proven to be unworkably slow. 
  636.  
  637.      Table 1 below shows the execution (user + system) time on a PDP-11/70 
  638. of the UNIX programs wc, grep, egrep, fgrep, sed, lex, and awk on the 
  639. following simple tasks; 
  640.  
  641.         1. count the number of lines.
  642.  
  643.         2. print all lines containing "doug"
  644.  
  645.         3. print all lines containing "doug", "ken", or "dmr".
  646.  
  647.         4. print the third field of each line.
  648.  
  649.         5. print the third and second fields of each line in that order
  650.  
  651.         6. append all lines containing "doug", "ken", and "dmr" to files
  652.            "jdoug", "jken", and "jdmr", respectivly.
  653.  
  654.         7. print each line prefixed by "line-number:".
  655.  
  656.         8. sum the fourth column of a table.
  657.  
  658. The program wc merely counts words, lines and characters in its input; we 
  659. have already mentioned the others.  In all cases the input was a file 
  660. containing 10,000 lines as created by the command ls -l; each line has the 
  661. form 
  662.  
  663.         -rw-rw-rw-   1 ava    123  Oct 15 17:05 xxx
  664.  
  665. The total length of this input is 452,960 characters.  Times for lex do not 
  666. include compile or load.
  667.  
  668.      As might be expected, awk is not as fast as the specialized tools wc, 
  669. sed, or the programs in the grep family, but is faster than the more 
  670. general tool lex.  In all cases, the tasks were about as easy to express as 
  671. awk programs as programs in these other languages; tasks involving fields 
  672. were conciderable easier to express as awk programs.  Some of the test 
  673. programs are shown in awk, sed and lex. 
  674.  
  675. [*]     UNIX is a Trademark of Bell Laboratories
  676.  
  677. References
  678.  
  679. [1]     K. Thompson and D. M. Ritchie, UNIX Programmers Manual.
  680.         Bell Laboratories (May 1975). Sixth Edition
  681.  
  682. [2]     B. W. Kernighan and D. M. Ritchie, The C Programming Language,
  683.         Prentice-Hall, Englewood Cliffs, New Jersey (1978)
  684.  
  685. [3]     M. E. Lesk, "Lex - A Lexical Analyzer Generator,"
  686.         Comp. Sci. Tech. Rep. No. 39,
  687.         Bell Laboratories, Murray Hill, New Jersey (October 1975)
  688.  
  689. [4]     S. C. Johnson, "Yacc - Yet Another Compiler-Compiler,"
  690.         Comp. Sci. Tech. Rep. No. 32,
  691.         Bell Laboratories, Murray Hill, New Jersey (July 1975)
  692.  
  693.  
  694.                                      Task 
  695.  
  696.    program     1       2       3       4       5       6       7       8
  697.  
  698.      wc       8.6
  699.     grep     11.7    13.1
  700.    egrep      6.2    11.5    11.6
  701.    fgrep      7.7    13.8    16.1
  702.     sed      10.2    11.6    15.8    29.0    30.5    16.1
  703.     lex      65.1   150.1   144.2    67.7    70.3   104.0    81.7    92.8
  704.     awk      15.0    25.6    29.9    33.3    38.9    46.4    71.4    31.1
  705.  
  706.            Table 1.  Execution Times of Programs  (Times are in sec.)
  707.  
  708.  
  709.      The programs for some of these jobs are shown below.  The lex programs 
  710. are generally too long to show.
  711.  
  712. AWK:
  713.  
  714. 1.      END { print NR }
  715.  
  716. 2.      /doug/
  717.  
  718. 3.      /ken|doug|dmr/
  719.  
  720. 4.      { print $3 }
  721.  
  722. 5.      { print $3, $2 }
  723.  
  724. 6.      /ken/   { print >"jken" }
  725.         /doug/  { print >"jdoug" }
  726.         /dmr/   { print >"jdmr" }
  727.  
  728. 7.      { print NR ": " $0 }
  729.  
  730. 8.              { sum = sum + $4 }
  731.         END     { print sum }
  732.  
  733. SED:
  734.  
  735. 1.      $=
  736.  
  737. 2.      /doug/p
  738.  
  739. 3.      /doug/p
  740.         /doug/d
  741.         /ken/p
  742.         /ken/d
  743.         /dmr/p
  744.         /dmr/d
  745.  
  746. 4.      /[^ ]* [ ]*[^ ]* [ ]*\([^ ]*\) .*/s//\1/p
  747.  
  748. 5.      /[^ ]* [ ]*\([^ ]*\) [ ]*\([^ ]*\) .*/s//\2 \1/p
  749.  
  750. 6       /ken/w jken
  751.         /doug/w jdoug
  752.         /dmr/w jdmr
  753.  
  754. LEX:
  755.  
  756. 1.      %{
  757.         int i;
  758.         %}
  759.         %%
  760.         \n  i++;
  761.         .   ;
  762.         %%
  763.         yywrap() {
  764.             printf("%d\n", i);
  765.         }
  766.  
  767. 2.      %%
  768.         ^.*doug.*$      printf("%s\n", yytext);
  769.         .       ;
  770.         \n      ;
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.                                    AWK 1985
  779.  
  780.                               Rob Duff 01-Mar-88
  781.                           Vancouver BC Canada V5L 1E5
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.      In 1985 Aho, Weinberger and Kernighan revised the definition of the AWK 
  789. programming language.  Some of the additions include user defined functions, 
  790. access to command line arguments, more arithmetic functions, and separate 
  791. input.  These changes reflected the use that the authors found other people 
  792. making of the language.
  793.  
  794.          ----------------end-of-author's-documentation---------------
  795.  
  796.                         Software Library Information:
  797.  
  798.                    This disk copy provided as a service of
  799.  
  800.                         The Public (Software) Library
  801.  
  802.          We are not the authors of this program, nor are we associated
  803.          with the author in any way other than as a distributor of the
  804.          program in accordance with the author's terms of distribution.
  805.  
  806.          Please direct shareware payments and specific questions about
  807.          this program to the author of the program, whose name appears
  808.          elsewhere in  this documentation. If you have trouble getting
  809.          in touch with the author,  we will do whatever we can to help
  810.          you with your questions. All programs have been tested and do
  811.          run.  To report problems,  please use the form that is in the
  812.          file PROBLEM.DOC on many of our disks or in other written for-
  813.          mat with screen printouts, if possible.  The P(s)L cannot de-
  814.          bug programs over the telephone.
  815.  
  816.          Disks in the P(s)L are updated monthly, so if you did not get
  817.          this disk  directly from the P(s)L,  you should be aware that
  818.          the files in this set may no  longer be the current versions.
  819.  
  820.          For a copy of the latest monthly software library newsletter
  821.          and a list of the 1,900+ disks in the library, call or write
  822.  
  823.                         The Public (Software) Library
  824.                               P.O.Box 35705 - F
  825.                            Houston, TX 77235-5705
  826.                                (713) 665-7017
  827.  
  828.